package org.itboys.core.controller.web;

import com.alibaba.fastjson.JSONObject;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayGoods;
import org.itboys.core.entity.mongo.SundayScene;
import org.itboys.core.service.mongo.web.SundayGoodsService;
import org.itboys.core.service.mongo.web.SundaySceneService;
import org.itboys.core.util.AccountIdHoldUtils;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/10/11
 * 描述：场景
 */
@Controller
@RequestMapping("/sunday/web/scene")
public class SundaySceneController extends BaseController {
    @Autowired
    private SundaySceneService sceneService;
    @Autowired
    private SundayGoodsService goodsService;

    /**
     * 主页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model) {
        return "/sunday/scene/index";
    }

    @RequestMapping(value = "/select", method = {RequestMethod.POST})
    public String select(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) {
        PageResult<SundayScene> result = sceneService.select(request);
        //2018年10月18日，增加分页需要从参数
        packagePageParam(model, result.getTotal(), request);
        model.addAttribute("scenes", result.getData());

        return "/sunday/scene/data";
    }

    /**
     * 新增或修改
     *
     * @param request
     * @param response
     * @param scene
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute SundayScene scene,
                     @RequestParam(value = "imageFile", required = false) MultipartFile imageFile,
                     @RequestParam(value = "detailImageFile", required = false) MultipartFile detailImageFile,
                     @RequestParam(value = "rollImageFiles", required = false) MultipartFile[] rollImageFiles
    ) throws IOException {
        sceneService.save(scene, imageFile, detailImageFile,rollImageFiles);
        ResponseMessage.success();
    }

    /**
     * 发布
     *
     * @param request
     * @param response
     * @param sceneId
     */
    @RequestMapping("/release")
    public void release(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "sceneId", required = true) Long sceneId
    ) {
        sceneService.release(sceneId);
        ResponseMessage.success();
    }

    /**
     * 下架
     *
     * @param request
     * @param response
     * @param sceneId
     */
    @RequestMapping("/downScene")
    public void downScene(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "sceneId", required = true) Long sceneId
    ) {
        sceneService.downScene(sceneId);
        ResponseMessage.success();
    }

    /**
     * 编辑页
     *
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String input(
            HttpServletRequest request,
            @RequestParam(value = "id", required = true) Long id,
            Model model) {

        SundayScene scene = sceneService.findOne(id);
        model.addAttribute("scene", scene);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("sceneId", scene.getId());
        List<SundayGoods> goodss = goodsService.list(param);
        model.addAttribute("goodss", goodss);
        return "/sunday/scene/edit";
    }

    /**
     * 新增页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            Model model
    ) {
        SundayScene scene = sceneService.findOne(0l);
        model.addAttribute("scene", scene);
        return "/sunday/scene/input";
    }


    /**
     * 删除
     *
     * @param request
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "id", required = true) Long id
    ) {
        sceneService.delete(id);
        ResponseMessage.success();
    }
}
